package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserShoppingCartMapper;
import com.sky.service.UserShoppingCartService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserShoppingCartServiceImpl implements UserShoppingCartService {
    @Autowired
    UserShoppingCartMapper userShoppingCartMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public void sub(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        //获取商品
        List<ShoppingCart> shoppingCartList = userShoppingCartMapper.list(shoppingCart);

        for (ShoppingCart cart : shoppingCartList) {
            Integer number = cart.getNumber();
            Long id = cart.getId();
            if (number > 1){
                //数量大于0时减一
                number = number -1;
                userShoppingCartMapper.sub(id,number);
            }else {
                //等于0时删除
                userShoppingCartMapper.deleteById(id);

            }

        }




    }

    @Override
    public void cleanShoppingCart() {
        Long currentId = BaseContext.getCurrentId();
        userShoppingCartMapper.cleanShoppingCart(currentId);
    }

    @Override
    public List<ShoppingCart> list() {
        List<ShoppingCart> list = userShoppingCartMapper.getShoppingCart();
        return list;
    }

    @Override
    public void SaveShoppingCart(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);

        //只能查询自己的购物车数据
        Long id = BaseContext.getCurrentId();
        shoppingCart.setUserId(id);
        //判断当前商品是否在购物车中
        List<ShoppingCart> shoppingCartList = userShoppingCartMapper.list(shoppingCart);


        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            //如果已经存在，就更新数量，数量加1
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            userShoppingCartMapper.updateNumberById(shoppingCart);
        } else {
            //如果不存在，插入数据，数量就是1
            //判断当前添加到购物车的是菜品还是套餐
            Long dishId = dto.getDishId();
            if (dishId != null) {
                //添加到购物车的是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //添加到购物车的是套餐
                SetmealVO setmeal = setmealMapper.getByid(dto.getSetmealId());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            userShoppingCartMapper.insert(shoppingCart);
        }


    }
}
